package com.yifi.module.jwflow.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.utils.DbUtils;

import java.util.List;

public class DesignService {


	public void saveNode(String id, JSONObject jsonObject)
	{
		String wf_id=id;
		String shape=jsonObject.getString("shape");
		String nid=jsonObject.getString("id");
		JSONObject position=jsonObject.getObject("position",JSONObject.class);
		JSONObject size=jsonObject.getObject("size",JSONObject.class);
		String position_x=position.getString("x");
		String position_y=position.getString("y");
		String width=size.getString("width");
		String height=size.getString("height");

		String text=jsonObject.getObject("attrs",JSONObject.class)
				.getObject("text",JSONObject.class).getString("text");

		Record record=new Record();
		record.set("id",nid);
		record.set("wf_id",wf_id);
		record.set("shape",shape);
		record.set("position_x",position_x);
		record.set("position_y",position_y);
		record.set("width",width);
		record.set("height",height);
		record.set("name",text);

		Db.save("jw_wf_node","id",record);
	}

	public void saveLink(String id, JSONObject jsonObject)
	{
		String nid=jsonObject.getString("id");
		String wf_id=id;
		JSONObject source_obj=jsonObject.getObject("source",JSONObject.class);
		JSONObject target_obj=jsonObject.getObject("target",JSONObject.class);
		String source_id=source_obj.getString("cell");
		String target_id=target_obj.getString("cell");
		String source_port=source_obj.getString("port");
		String target_port=target_obj.getString("port");

		Record record=new Record();
		record.set("id",nid);
		record.set("wf_id",wf_id);
		record.set("source_id",source_id);
		record.set("target_id",target_id);
		record.set("label","");
		record.set("condition","");

		Db.save("jw_wf_link","id",record);
	}


	public void saveNodes(String wf_id, JSONArray array)
	{
		String delIds="''";
		//String sql="delete from jw_wf_node where wf_id='"+wf_id+"'";
		//Db.update(sql);
		String sql="";
		for (int i = 0; i < array.size(); i++) {
			JSONObject jsonObject=array.getJSONObject(i);
			String shape=jsonObject.getString("shape");
			String nid=jsonObject.getString("id");
			JSONObject position=jsonObject.getObject("position",JSONObject.class);
			JSONObject size=jsonObject.getObject("size",JSONObject.class);
			String position_x=position.getString("x");
			String position_y=position.getString("y");
			String width=size.getString("width");
			String height=size.getString("height");

			//清除关闭工具
			if(jsonObject.containsKey("tools"))
			{
				jsonObject.remove("tools");
			}

			JSONObject attrsJson=jsonObject.getObject("attrs",JSONObject.class);
			String text=attrsJson.getObject("text",JSONObject.class).getString("text");

			delIds+=",'"+nid+"'";
			sql="select * from jw_wf_node where id='"+nid+"'";
			Record record=Db.findFirst(sql);
			boolean isedit=true;
			if(record==null)
			{
				isedit=false;
				record=new Record();
				record.set("id",nid);
				record.set("wf_id",wf_id);

				//初始化其他节点的值，以防取出数据的时候null的问题
				record.set("detail_url","");
				record.set("deal_action","");
				record.set("app_url","");
				record.set("deal_back",0);
				record.set("deal_attr",0);
				record.set("deal_type",0);
				record.set("deal_type_rate",0);
				record.set("by_group","0");
				record.set("is_back",0);
				record.set("is_filter",0);
				record.set("is_cancel",0);
			}
			record.set("shape",shape);
			record.set("position_x",position_x);
			record.set("position_y",position_y);
			record.set("width",width);
			record.set("height",height);
			record.set("name",text);
			DbUtils.save("jw_wf_node","id",record,isedit);
		}
		sql="delete from jw_wf_node where wf_id ='"+wf_id+"' and id not in ("+delIds+")";
		Db.update(sql);
	}

	public void saveLinks(String wf_id, JSONArray array)
	{
		String delIds="''";
		//String sql="delete from jw_wf_link where wf_id='"+wf_id+"'";
		//Db.update(sql);
		String sql="";
		for (int i = 0; i < array.size(); i++) {
			JSONObject jsonObject = array.getJSONObject(i);
			String nid = jsonObject.getString("id");
			JSONObject source_obj = jsonObject.getObject("source", JSONObject.class);
			JSONObject target_obj = jsonObject.getObject("target", JSONObject.class);
			String source_id = source_obj.getString("cell");
			String target_id = target_obj.getString("cell");
			String source_port = source_obj.getString("port");
			String target_port = target_obj.getString("port");

			delIds+=",'"+nid+"'";
			sql="select * from jw_wf_link where id='"+nid+"'";
			Record record=Db.findFirst(sql);
			boolean isedit=true;
			if(record==null)
			{
				isedit=false;
				record=new Record();
				record.set("id",nid);
				record.set("wf_id",wf_id);
				record.set("label", "");
				record.set("condition", "");
			}
			record.set("source_id", source_id);
			record.set("target_id", target_id);

			DbUtils.save("jw_wf_link", "id", record,isedit);
		}
		sql="delete from jw_wf_link where wf_id ='"+wf_id+"' and id not in ("+delIds+")";
		Db.update(sql);
	}

	public JSONObject getJson(String wf_id)
	{
		JSONObject jsonObject=new JSONObject();

		JSONArray jsonArray=new JSONArray();
		String sql="select * from jw_wf_node where wf_id='"+wf_id+"'";
		List<Record> list=Db.find(sql);
		for (Record rec:list) {
			JSONObject jobj=new JSONObject();
			jobj.put("shape",rec.getStr("shape"));
			jobj.put("id",rec.getStr("id"));

			JSONObject jobjP=new JSONObject();
			jobjP.put("x",rec.getInt("position_x"));
			jobjP.put("y",rec.getInt("position_y"));
			jobj.put("position",jobjP);
			jobjP=new JSONObject();
			jobjP.put("width",rec.getInt("width"));
			jobjP.put("height",rec.getInt("height"));
			jobj.put("size",jobjP);

			JSONObject jobjT=new JSONObject();
			jobjT.put("text",rec.getStr("name"));
			jobjP=new JSONObject();
			jobjP.put("text",jobjT);
			jobj.put("attrs",jobjP);
			jsonArray.add(jobj);
		}

		sql="select * from jw_wf_link where wf_id='"+wf_id+"'";
		List<Record> list_link=Db.find(sql);
		for (Record rec:list_link) {
			JSONObject jobj=new JSONObject();
			jobj.put("shape","edge");
			jobj.put("id",rec.getStr("id"));

			JSONObject jobjP=new JSONObject();
			jobjP.put("cell",rec.getStr("source_id"));
			jobj.put("source",jobjP);
			jobjP=new JSONObject();
			jobjP.put("cell",rec.getStr("target_id"));
			jobj.put("target",jobjP);

			jsonArray.add(jobj);
		}

		jsonObject.put("cells",jsonArray);

		return jsonObject;
	}

	public String getJsonData(String id)
	{
		String sql="select json_data from jw_wf_code where id=?";
		return Db.queryStr(sql,id);
	}
}
